package cn.miaoyao.miaoyaoms.service.impl;
import cn.miaoyao.miaoyaoms.entity.Limits;
import cn.miaoyao.miaoyaoms.entity.User;
import cn.miaoyao.miaoyaoms.mapper.UserMapper;
import cn.miaoyao.miaoyaoms.service.LimitsService;
import cn.miaoyao.miaoyaoms.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sun
 * @since 2023-03-28
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private LimitsService limitsService;
    /**
     * 重写save方法，当用户注册的时候设置其默认权限。
     * @param user
     * @return
     */
    @Override
    public boolean save(User user) {
        try {
            if (user != null) {
                userMapper.insert(user);
                if ("1".equals(user.getAdmin())) {//管理员
                    Limits limits = new Limits(user.getUid(), 0, 0, 1, 1, 1, 1, 0,0);
                    limitsService.save(limits);
                } else {
                    //保存用户的时候，同时将对应的权限也保存到权限表中。
                    Limits limits = new Limits(user.getUid(), 0, 0, 0, 0, 0, 1, 0,0);
                    limitsService.save(limits);
                }
                return true;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 重写父类的删除方法。
     * @param id
     * @return
     */
    @Override
    public boolean removeById(Serializable id) {
        int i = userMapper.deleteById(id);
        boolean b = limitsService.removeById(id);
        if(i==1&&b){
            return true;
        }
        return false;
    }

}
